package cn.edu.jxau.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.jxau.common.User;
import cn.edu.jxau.exception.ServiceException;
import cn.edu.jxau.service.UserService;
import cn.edu.jxau.service.impl.UserServiceImpl;
import cn.edu.jxau.utils.CommonUtils;

public class UserServlet extends BaseServlet{

	UserService userService = new UserServiceImpl();
	private static final String SESSION_USER = "session_user";
	
	public String login(HttpServletRequest req, HttpServletResponse resp) {
		
		// 封装表单数据到form中
		User from = CommonUtils.toBean(req.getParameterMap(), User.class);
		
		try {
			User user = userService.queryUser(from);
			user.setPassword(null);
			// 使用request对象的getSession()获取session，如果session不存在则创建一个
			HttpSession session = req.getSession();
			// 将数据存储到session中
			session.setAttribute(SESSION_USER, user);
			return "f:/WEB-INF/pages/admin.jsp";
		} catch (ServiceException e) {
			req.setAttribute("msg", e.getMessage());
			return "f:/login.jsp";
		}
	}
	
}
